package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
)

// 16000 获取玩家特殊箱子内容
func (p *Player) SpBoxInfoGetRequest(req *sp.SpBoxInfoGetRequest) (res *sp.SpBoxInfoGetResponse) {
	userSpContent := p.User.GetUserSpBoxContent()
	if userSpContent == nil {
		err := errors.NewErrcode(data.Errcode.NotFound, "找不到玩家SpBox数据")
		res = &sp.SpBoxInfoGetResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.SpBoxInfoGetResponse{
		InfoGroup: lib.FormatSpBoxContent(p.User, userSpContent.SpBoxContent),
	}
	return
}

// 16001 玩家特殊箱子内容推送，一般用于登录 s-c
func (p *Player) PushUserSpBoxInfo() {
	userSpContent := p.User.GetUserSpBoxContent()

	if userSpContent == nil {
		// 没有数据就push个空--客户端接收到列表为空，就会认为没有数据
		pushRes := &sp.SpBoxInfoPushRequest{}
		p.Send(pushRes)
	} else {
		pushRes := &sp.SpBoxInfoPushRequest{
			InfoGroup: lib.FormatSpBoxContent(p.User, userSpContent.SpBoxContent),
		}
		p.Send(pushRes)
	}
}

// 16002 玩家选择特殊箱子道具
func (p *Player) SpBoxUserChoiceItemRequest(req *sp.SpBoxUserChoiceItemRequest) (res *sp.SpBoxUserChoiceItemResponse) {
	goodTid, err := lib.GiveSpBoxItemByUserChoice(p.User, req.ContentTid, req.ChoiceId)
	if err != nil {
		// 找不到缓存/找不到contentTid对应内容/找不到choiceTid对应道具，或者对应道具Tid为0
		res = &sp.SpBoxUserChoiceItemResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	res = &sp.SpBoxUserChoiceItemResponse{
		ContentTid: req.ContentTid,
		ChoiceId:   req.ChoiceId,
		GoodTid:    goodTid,
	}
	//给玩家添加道具
	uProp, _, _, err := lib.GiveGoodsByKv("SpBoxUserChoiceItemRequest", p.User, m.BaseKV{Key: goodTid, Val: 1}, p.Send)
	// 有成就完成，进行通知
	achLst := lib.AchievComplete(p.User)
	if len(achLst) > 0 {
		p.Send(&sp.AchievementNoticeRequest{
			Lst: achLst,
		})
	}
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}
